﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Phoenix.Xna.Framework.Components.Compositors
{
    public class TextCompositor : Compositor
    {
        private string _asset;

        protected SpriteFont SpriteFont { get; set; }
        public string FontAsset
        {
            get { return _asset; }
            set
            {
                if (!string.IsNullOrEmpty(_asset))
                {
                    Engine.Instance.Content.Release(_asset);
                }

                _asset = value;
                SpriteFont = Engine.Instance.Content.Load<SpriteFont>(_asset);
            }
        }
        public string Text { get; set; }

        public TextCompositor(string font)
            : base()
        {
            _asset = font;
        }

        public TextCompositor(string id, string font)
            : base(id)
        {
            _asset = font;
        }

        #region Compositor Members

        public override void LoadContent()
        {
            SpriteFont = Engine.Instance.Content.Load<SpriteFont>(_asset);
            base.LoadContent();
        }

        public override void UnloadContent()
        {
            Engine.Instance.Content.Release(_asset);
            base.UnloadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            if (Visible)
            {
                if (SpriteFont != null && !string.IsNullOrEmpty(Text))
                {
                    SpriteBatch.DrawString(SpriteFont, Text, Position, Color, Rotation, Origin, Scale, Effect, LayerDepth);
                }
            }
        }

        #endregion
    }
}
